<?php
namespace App\Http\Controllers\AppApi\My;

use App\Jobs\Fun;
use App\Models\AppVersionModel;
use App\Services\Admin\JobStaffService;
use App\Services\Admin\UserService;
use App\Utilities\Upload;
use Illuminate\Http\Request;
use Hash;

class MySetController extends BaseController
{
    /**
     * 我的主页接口控制器
     * 制作者：九哥
     */

    protected $user;
    protected $job;

    public function __construct()
    {
        parent::__construct();
        $this->user = new UserService();
        $this->job = new JobStaffService();
    }

    /**
     * 获取用户信息
     * /api/my/myset/userinfo
     */
    public function getUserInfo(Request $request)
    {
        ///exit('sssssssss');

        if ($request->app_access!=$this->app_access) {
            return $this->toJson(-1);
        }
        $token = $request->token;
        $type = $request->type;
        $cate = $request->cate;
        if (!$token ) {//|| !in_array($type,[1,2,3,4])
            return $this->toJson(-2);
        }

        $uid = $this->accessGet($token);
        if (!$uid) { return $this->toJson(-13); }
        $model = $this->user->getOne($uid);
        if (!$model) { return $this->toJson(-3); }
        $nickname = mb_strlen($model->name)>10 ?
            mb_substr($model->name,0,10).'...' : $model->name;
        $head = $model->head ? $model->head : '';
        $dataArr = array(
            'nickname' => $nickname,
            'head' =>Fun::pImg($head),
            'sex' => $model->sex,
            'typeName' => $model->typeName,
        );
        return $this->toJson(0,$dataArr);
    }

    /**
     * 头像更新
     */
    public function setHeadUpload(Request $request)
    {
        if ($request->app_access!=$this->app_access) {
            return redirect('/h5/error.html?errMsg=接口权限错误');
        }
        $token = $request->token;
        $type = $request->type;
        $cate = $request->cate;
        $viewJump = $request->viewJump;
       /* if (!$token) {
            return redirect('/h5/error.html?errMsg=参数错误');
        }*/
        $uid = $this->accessGet($token);
        if (!$uid) {
            return redirect('/h5/error.html?errMsg=用户未登录或未注册');
        }
        if (!isset($request->file)) {
            return redirect('/h5/error.html?errMsg=文件没有上传');
        }
        $model = $this->user->getOne($uid);
        $head = '';
        if (isset($request->file)) {
            $resFile = Upload::uploadImgByApi($request,'file');
            if ($resFile['code']!=0) {
                return redirect('/h5/error.html?errMsg='.$resFile['msg']);
            }
            if ($model && $model->head) {
                if ($model->head && file_exists(ltrim($model->head,'/'))) {
                    unlink(ltrim($model->head,'/'));
                }
            }
            $head = $resFile['filePath'];
        }
        $head = $head ? $head : $model->head;
        $dataArr = array(
            'name' => $model->name,
            'head' => $head,
            'sex' => $model->sex,
        );
        $this->user->modify($dataArr,$uid);
        return redirect($viewJump);
    }

    /**
     * 设置昵称
     */
    public function setNick(Request $request)
    {
        if ($request->app_access!=$this->app_access) {
            return $this->toJson(-1);
        }
        $token = $request->token;
        $type = $request->type;
        $cate = $request->cate;
        $nickname = $request->nickname;
        if (!$token || !$nickname||$nickname=='') {
            return $this->toJson(-2);
        }
        $uid = $this->accessGet($token);
        if (!$uid) { return $this->toJson(-13); }
        $model = $this->user->getOne($uid);
        if (!$model) { return $this->toJson(-3); }
        $dataArr = array(
            'name' => $nickname,
            'head' => $model->head,
            'sex' => $model->sex,
        );
        $this->user->modify($dataArr,$uid);
      //  var_dump($model->jiguang_id);exit;
        \App\Services\Jiguang\JMessageApi::upNameNick($model->jiguang_id,$nickname);
        return $this->toJson(0,$dataArr);
    }

    /**
     * 设置性别
     */
    public function setSex(Request $request)
    {
        if ($request->app_access!=$this->app_access) {
            return $this->toJson(-1);
        }
        $token = $request->token;
        $type = $request->type;
        $cate = $request->cate;
        $sex = $request->sex;
        if (!$token || !$sex) {
            return $this->toJson(-2);
        }
        $uid = $this->accessGet($token);
        if (!$uid) { return $this->toJson(-13); }
        $model = $this->user->getOne($uid);
        if (!$model) { return $this->toJson(-3); }
        //if ($model->sex) { return $this->toJson(-18); }
        $dataArr = array(
            'name' => $model->name,
            'head' => $model->head,
            'sex' => $sex,
        );
        $this->user->modify($dataArr,$uid);
        return $this->toJson(0,$dataArr);
    }

    /**
     * 设置密码
     */
    public function setPwd(Request $request)
    {
        if ($request->app_access!=$this->app_access) {
            return $this->toJson(-1);
        }
        $token = $request->token;
        $type = $request->type;
        $cate = $request->cate;
        $pwd = $request->pwd;
        $pwd2 = $request->pwd2;
        if (!$token || !$pwd || !$pwd2) {
            return $this->toJson(-2);
        }
        $uid = $this->accessGet($token);
        if (!$uid) { return $this->toJson(-13); }
        $model = $this->user->getOne($uid);
        if (!$model) { return $this->toJson(-3); }
        if (!(Hash::check($pwd,$model->password))) {
            return $this->toJson(-10);
        }
        if (!preg_match('/[0-9a-zA-Z_]{5,20}/',$pwd2)) {
            return $this->toJson(-12);
        }
        $password = Hash::make($pwd2);
        $this->user->setPwd($uid,$password);

        return $this->toJson(0);
    }

//    /**
//     * 公司招聘，获取
//     */
//    public function getComJobList(Request $request)
//    {
//        if ($request->app_access!=$this->app_access) {
//            return $this->toJson(-1);
//        }
//        $token = $request->token;
//        $type = $request->type;
//        $cate = $request->cate;
//        if (!$token) {
//            return $this->toJson(-2);
//        }
//        $uid = $this->accessGet($token);
//        if (!$uid) { return $this->toJson(-13); }
//        $resJob = $this->job->getIndexByCid($uid);
//        if (!$resJob) { return $this->toJson(-3); }
//        $dataArr = $resJob;
//        return $this->toJson(0,$dataArr);
//    }
//
//    /**
//     * 工人应聘，获取
//     */
//    public function getStaffJobList(Request $request)
//    {
//        if ($request->app_access!=$this->app_access) {
//            return $this->toJson(-1);
//        }
//        $token = $request->token;
//        $type = $request->type;
//        $cate = $request->cate;
//        if (!$token) {
//            return $this->toJson(-2);
//        }
//        $uid = $this->accessGet($token);
//        if (!$uid) { return $this->toJson(-13); }
//        $resJob = $this->job->getStaffIndexByCid($uid);
//        if (!$resJob) { return $this->toJson(-3); }
//        $dataArr = $resJob;
//        return $this->toJson(0,$dataArr);
//    }
//
//    /**
//     * 工人应聘，状态设置
//     */
//    public function setJobStaffStatus(Request $request)
//    {
//        if ($request->app_access!=$this->app_access) {
//            return $this->toJson(-1);
//        }
//        if ($request->app_access!=$this->app_access) {
//            return $this->toJson(-1);
//        }
//        $token = $request->token;
//        $type = $request->type;
//        $cate = $request->cate;
//        $id = $request->id;
//        $status = $request->status;
//        if (!$token) {
//            return $this->toJson(-2);
//        }
//        $uid = $this->accessGet($token);
//        if (!$uid) { return $this->toJson(-13); }
//        $resJob = $this->job->setStaffStatus($uid,$status);
//        if (!$resJob) { return $this->toJson(-3); }
//        $dataArr = $resJob;
//        return $this->toJson(0,$dataArr);
//    }

    /**
     * APP版本更新
     * api/app/version
     */
    public function getAppVersion(Request $request)
    {
        if ($request->app_access!=$this->app_access) {
            return $this->toJson(-1);
        }
        $appIndentity = $request->appIndentity;
        $dataArr = \Illuminate\Support\Facades\Cache::remember('api/app/version',
            1000, function()use ($appIndentity) {
            $query = AppVersionModel::where('del', 0);
            if ($appIndentity == 'ios') {
                $query = $query->where('genre', 2);
            } else {
                $query = $query->where('genre', 1);
            }
            $model = $query->first();
            if (!$model) {
                return $this->toJson(-3);
            }
            $dataArr = array(
                'version' => $model->version,
                'url' => $model->url,
            );
            return $dataArr;
        });

        return $this->toJson(0,$dataArr);
    }
}